package org.jeecg.modules.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.weather.entity.Weather;
import org.jeecg.modules.weather.service.IWeatherService;
import org.jeecg.modules.wxchar.util.HttpClientUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * 示例不带参定时任务
 * 
 * @Author Scott
 */
@Slf4j
public class JeiqiJob implements Job {

	@Autowired
	private IWeatherService weatherService;

	private static final String URL = "https://api.jisuapi.com/jieqi/query";
	private static final String APPKEY = "1c96a7ffab6423dc";

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		Map<String, String> param = new HashMap<>();
		param.put("appkey", APPKEY);
		// 发送请求
		String result = HttpClientUtil.doGet(URL, param);
		Weather weather = weatherService.getWeather();
		if(weather == null ){
			weather = new Weather();
			weather.setJieqi(result);
			weatherService.save(weather);
		}else {
			weather.setJieqi(result);
			weatherService.updateById(weather);
		}
	}
}
